package org.road0329.dlt698.axdr;

import org.road0329.dlt698.ReverseByteArrayInputStream;
import org.road0329.dlt698.ReverseByteArrayOutputStream;
import org.road0329.dlt698.asn1.axdr.AxdrSequenceOf;
import org.road0329.dlt698.asn1.axdr.AxdrType;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

// Assuming the existence of equivalent Java classes for AxdrType, ReverseByteArrayInputStream, and ReverseByteArrayOutputStream
// and that RETOMDDATA is already translated to Java.

public class ProxyActionResponse extends AxdrType {
    private byte[] dataCode = null;
    private TSA tsa = null;
    private SubSeqOfResult result = null;

    public ProxyActionResponse() {
    }

    public void set_dataCode(byte[] dataCode) {
        this.dataCode = dataCode;
    }

    public void set_all(TSA tsa, SubSeqOfResult result) {
        this.tsa = tsa;
        this.result = result;
    }

    @Override
    public int decode(ReverseByteArrayInputStream input) throws Exception {
        int codeLength = 0;
        this.tsa = new TSA();
        codeLength += this.tsa.decode(input);
        this.result = new SubSeqOfResult();
        codeLength += this.result.decode(input);
        return codeLength;
    }

    @Override
    public int encode(ReverseByteArrayOutputStream output) throws Exception {
        int codeLength = 0;
        if (this.dataCode != null) {
            codeLength = this.dataCode.length;
            for (int i = this.dataCode.length - 1; i >= 0; i--) {
                output.write(this.dataCode[i]);
            }
        } else {
            if (this.result != null && this.tsa != null) {
                codeLength += this.result.encode(output);
                codeLength += this.tsa.encode(output);
            } else {
                throw new RuntimeException("result || tsa is null");
            }
        }
        return codeLength;
    }

    public void encodeAndSave(int encodingSizeGuess) throws Exception {
        ReverseByteArrayOutputStream revOStream = new ReverseByteArrayOutputStream();
        revOStream.setBufSize(encodingSizeGuess);
        this.encode(revOStream);
        this.dataCode = revOStream.toByteArray();
    }

    @Override
    public String toString() {
        if (this.tsa != null && this.result != null) {
            return "sequence: {" + "tsa: " + this.tsa + ", result: " + this.result + "}";
        } else {
            return "result || tsa is null";
        }
    }

    class SubSeqOfResult extends AxdrSequenceOf<RETOMDDATA> {
        @Override
        public RETOMDDATA createListElement() {
            return new RETOMDDATA();
        }
    }
}



